package io.renren.modules.echarts.service.Impl;


import io.renren.modules.dataDisplay02.entity.chidaozaotui;
import io.renren.modules.echarts.dao.TeacherPortraitDao;
import io.renren.modules.echarts.entity.*;
import io.renren.modules.echarts.service.TeacherPortraitService;
import io.renren.modules.jxgk.common.ZTreeNode;
import io.renren.modules.jxgk.dao.CollegeInfoDao;
import io.renren.modules.jxgk.dao.TeacherInfoDao;;
import io.renren.modules.jxgk.entity.CollegeInfoEntity;
import io.renren.modules.jxgkphone.dao.ClassCourseMapper;
import io.renren.modules.jxgkphone.entity.ClassCourse;
import io.renren.modules.jxgkphone.entity.CourseDate;
import io.renren.modules.jxgkphone.entity.DateInfo;
import io.renren.modules.jxgkphone.entity.TeacherInfo;
import io.renren.modules.jxgkphone.service.CourseDateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherPortraitServiceImpl implements TeacherPortraitService {

    @Autowired
    CollegeInfoDao collegeInfoDao;
    @Autowired
    TeacherInfoDao teacherInfoDao;
    @Autowired
    TeacherPortraitDao teacherPortraitDao;
    @Autowired
    ClassCourseMapper classCourseMapper;

    @Override
    public List<ZTreeNode> getTeacherTree() {
        List<CollegeInfoEntity> cie=collegeInfoDao.getAllStudentCollege2();
        List<ZTreeNode> zTreeNodeList= new ArrayList<>();
        for(CollegeInfoEntity c:cie)
        {
            ZTreeNode zTreeNode=new ZTreeNode();
            zTreeNode.setMenuId(c.getCollegeName());
            zTreeNode.setName(c.getCollegeName());
            zTreeNode.isNocheck(true);
            zTreeNodeList.add(zTreeNode);
            Map map=new HashMap();
            map.put("college",c.getCollegeName());
            List<TeacherInfo> teachherList=teacherInfoDao.getTeacherByCollege(map);
            for(TeacherInfo p:teachherList)
            {
                ZTreeNode zTreeNode1=new ZTreeNode();
                zTreeNode1.setMenuId(p.getId());
                zTreeNode1.setName(p.getName());
                zTreeNode1.setParentId(c.getCollegeName());
                zTreeNode1.setParentName(c.getCollegeName());
                zTreeNode1.isNocheck(true);
                zTreeNodeList.add(zTreeNode1);
            }
        }
        return zTreeNodeList;
    }

    @Override
    public Map<String, List> getTimeLine(Map<String, Object> params) {
        String valid=ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        List<TeachLengthTimeEntity> classDiaryList=teacherPortraitDao.getTimeLine(params);
        Map<String, List> map=new HashMap<>();
        List<String> xData=new ArrayList<>();
        List<String> seriesData=new ArrayList<>();
        for(int i=0;i<classDiaryList.size();i++)
        {
            xData.add(classDiaryList.get(i).getDate());
            seriesData.add(classDiaryList.get(i).getTeachLength());
        }
        map.put("xData",xData);
        map.put("seriesData",seriesData);
        return map;
    }

    @Override
    public Map<String, List> getTimeLineByCollege(Map<String, Object> params) {
        String valid=ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        List<TeachLengthTimeEntity> classDiaryList=teacherPortraitDao.getTimeLineByCollege(params);
        Map<String, List> map=new HashMap<>();
        List<String> xData=new ArrayList<>();
        List<String> seriesData=new ArrayList<>();
        for(int i=0;i<classDiaryList.size();i++)
        {
            xData.add(classDiaryList.get(i).getDate());
            seriesData.add(classDiaryList.get(i).getTeachLength());
        }
        map.put("xData",xData);
        map.put("seriesData",seriesData);
        return map;
    }


    @Override
    public Map<String, List> getiMatchContentRateTimeLine(Map<String, Object> params) {
        String valid=ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        List<PieEntity> classDiaryList=teacherPortraitDao.getiMatchContentRateTimeLine(params);
        Map<String, List> map=new HashMap<>();
        List<String> xData=new ArrayList<>();
        List<String> seriesData=new ArrayList<>();
        for(int i=0;i<classDiaryList.size();i++)
        {
            xData.add(classDiaryList.get(i).getDateTime());
            Double d=classDiaryList.get(i).getMatcha()/classDiaryList.get(i).getTotal()*100;
            seriesData.add(d.toString());
        }
        map.put("xData",xData);
        map.put("seriesData",seriesData);
        return map;
    }

    @Override
    public Map getTeacherChidao(Map<String, Object> params) {
        String valid=ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        List<chidaozaotui> chidaos=  teacherPortraitDao.getTeacherChidao(params);
        Map<String, List> map=new HashMap<>();
        List<String> xData=new ArrayList<>();
        List<String> seriesData=new ArrayList<>();
        List<String> teacherId=new ArrayList<>();
        for(int i=chidaos.size();i>0;i--)
        {
            if (!"0".equals(chidaos.get(i - 1).getTotal())) {
                xData.add(chidaos.get(i - 1).getName());
                seriesData.add(chidaos.get(i - 1).getTotal());
                teacherId.add(chidaos.get(i - 1).getId());
            }
        }
        map.put("teacherId",teacherId);
        map.put("xData",xData);
        map.put("seriesData",seriesData);
        return map;
    }

    @Override
    public Map<String, List> getTeacherZaotui(Map<String, Object> params) {
        String valid=ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        List<chidaozaotui> zaotui= teacherPortraitDao.getTeacherZaotui(params);
        Map<String, List> map=new HashMap<>();
        List<String> xData=new ArrayList<>();
        List<String> seriesData=new ArrayList<>();
        List<String> teacherId=new ArrayList<>();
        for(int i=zaotui.size();i>0;i--)
        {
            if (!"0".equals(zaotui.get(i - 1).getTotal())) {
                xData.add(zaotui.get(i - 1).getName());
                seriesData.add(zaotui.get(i - 1).getTotal());
                teacherId.add(zaotui.get(i - 1).getId());
            }
        }
            map.put("xData",xData);
        map.put("teacherId",teacherId);
        map.put("seriesData",seriesData);
        return map;
    }

    @Override
    public Map<String, List> getiMatchContentRateOfCollege(Map<String, Object> params) {
        String valid=ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        List<chidaozaotui> classDiaryList=teacherPortraitDao.getiMatchContentRateOfCollege(params);
        Map<String, List> map=new HashMap<>();
        List<String> xData=new ArrayList<>();
        List<String> seriesData=new ArrayList<>();
        for(int i=0;i<classDiaryList.size();i++)
        {
            xData.add(classDiaryList.get(i).getName());
            seriesData.add(classDiaryList.get(i).getTotal());
        }
        map.put("xData",xData);
        map.put("seriesData",seriesData);
            return map;
    }

    public String ValidDatesConverter(Map<String, Object> params){
        Map<String, Object> inputParams=new HashMap<>();
        //将离散日期转化为具体日期
        //获取离散数据
        String yearData=(String) params.get("academicYear");
        String termData=(String) params.get("academicTerm");
        String weekData=(String) params.get("academicWeek");
        String dayData=(String) params.get("academicDay");
        if(yearData!=null) {
            String[] years = yearData.split(",");
            String[] terms = termData.split(",");
            String[] weeks = weekData.split(",");
            String[] days = dayData.split(",");
            //初始化具体日期数组
            StringBuffer dates = new StringBuffer();
            List<String> result = new ArrayList<>();
            //遍历结果集
            for (int i = 0; i < years.length; i++) {
                inputParams.put("courseAcademicYear", years[i].replace("\'", ""));
                for (int j = 0; j < terms.length; j++) {
                    inputParams.put("courseAcademicTerm", terms[j].replace("\'", ""));
                    for (int k = 0; k < weeks.length; k++) {
                        inputParams.put("courseWhichWeek", weeks[k].replace("\'", ""));
                        for (int l = 0; l < days.length; l++) {
                            inputParams.put("courseWhichDay", days[l].replace("\'", ""));
                            List<String> date = classCourseMapper.findDate(inputParams);
                            result.addAll(date);
                        }
                    }
                }
            }
            for (String date : result) {
                //装入具体日期数组
                dates.append("\'");
                dates.append(date);
                dates.append("\'");
                dates.append(",");
            }

            //删除结尾处的逗号
            dates.deleteCharAt(dates.length() - 1);
            String datesData = dates.toString();
            return datesData;
        }else
        return "";
    }
    @Override
    public List<AttendanceDate> getTeacherAttendancePie(Map<String, Object> params) {
        String valid=ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        List<TeacherAttendancePie>   teacherAttendancePie=teacherPortraitDao.getTeacherAttendancePie(params);
        List<AttendanceDate> list=new ArrayList<>();
        AttendanceDate zhengchang=new AttendanceDate();
        AttendanceDate chidao=new AttendanceDate();
        AttendanceDate zaotui=new AttendanceDate();
        AttendanceDate tiaozheng=new AttendanceDate();
        zhengchang.setName("正常");
        chidao.setName("迟到");
        zaotui.setName("早退");
        tiaozheng.setName("调整");
        if(teacherAttendancePie.get(0)!=null) {
            zhengchang.setValue(teacherAttendancePie.get(0).getZhengchang().toString());
            chidao.setValue(teacherAttendancePie.get(0).getChidao().toString());
            zaotui.setValue(teacherAttendancePie.get(0).getZaotui().toString());
            tiaozheng.setValue(teacherAttendancePie.get(0).getTiaozheng().toString());
        }
        list.add(zhengchang);
        list.add(chidao);
        list.add(zaotui);
        list.add(tiaozheng);
        return list;
    }

    @Override
    public Map<String, Object> getTeacherAttendanceOfCollege(Map<String, Object> params) {
        String valid=ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        List<CollegeInfoEntity> collageInfo=collegeInfoDao.getAllStudentCollege();
        List<TeacherAttendancePie>   teacherAttendancePie=teacherPortraitDao.getTeacherAttendancePieGroupByCollege(params);
        changePercentage(teacherAttendancePie   );
        List<String> collageName =new ArrayList<>();
        List<EchartsDate> echartsDates=new ArrayList<>();
        EchartsDate zhengchang=new EchartsDate();
        zhengchang.setName("正常");
        zhengchang.setStack("总量1");
        zhengchang.setType("bar");
        List<String> zhengchangData=new ArrayList<>();
        EchartsDate chidao=new EchartsDate();
        chidao.setName("迟到");
        chidao.setStack("总量");
        chidao.setType("bar");
        List<String> chidaoData=new ArrayList<>();
        EchartsDate zaotui=new EchartsDate();
        zaotui.setName("早退");
        zaotui.setStack("总量3");
        zaotui.setType("bar");
        List<String> zaotuiData=new ArrayList<>();
        EchartsDate tiaozheng=new EchartsDate();
        tiaozheng.setName("调整");
        tiaozheng.setType("bar");
        tiaozheng.setStack("总量4");
        List<String> tiaozhengData=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
//        for(CollegeInfoEntity c:collageInfo)
//        {
//            collageName.add(c.getCollegeName());
//        }
        for(TeacherAttendancePie t:teacherAttendancePie)
        {

                    zhengchangData.add(t.getZhengchang().toString());
                    chidaoData.add(t.getChidao().toString());
                    zaotuiData.add(t.getZaotui().toString());
                    tiaozhengData.add(t.getTiaozheng().toString());
                collageName.add(t.getName());
        }
        chidao.setData(chidaoData);
        zhengchang.setData(zhengchangData);
        zaotui.setData(zaotuiData);
        tiaozheng.setData(tiaozhengData);
        echartsDates.add(zhengchang);
        echartsDates.add(chidao);
        echartsDates.add(zaotui);
        echartsDates.add(tiaozheng);

        map.put("collageName",collageName);
        map.put("data",echartsDates);
    //    map.put("collageRate",collageRate);
        return map;
    }

    @Override
    public Map<String, Object> getTeacherAttendanceOfCollege01(Map<String, Object> params) {
        String valid=ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        List<CollegeInfoEntity> collageInfo=collegeInfoDao.getAllStudentCollege2();
        List<TeacherAttendancePie>   teacherAttendancePie=teacherPortraitDao.getTeacherAttendancePieGroupByCollege(params);
        changePercentage(teacherAttendancePie);
        List<String> collageName =new ArrayList<>();
        List<EchartsDate> echartsDates=new ArrayList<>();
        EchartsDate zhengchang=new EchartsDate();
        zhengchang.setName("正常");
        zhengchang.setStack("总量1");
        zhengchang.setType("bar");
        zhengchang.setBarWidth("18");
        List<String> zhengchangData=new ArrayList<>();
        EchartsDate chidao=new EchartsDate();
        chidao.setName("迟到");
        chidao.setStack("总量");
        chidao.setType("bar");
        chidao.setBarWidth("18");
        List<String> chidaoData=new ArrayList<>();
        EchartsDate zaotui=new EchartsDate();
        zaotui.setName("早退");
        zaotui.setStack("总量3");
        zaotui.setType("bar");
        zaotui.setBarWidth("18");
        List<String> zaotuiData=new ArrayList<>();
        EchartsDate tiaozheng=new EchartsDate();
        tiaozheng.setName("调整");
        tiaozheng.setType("bar");
        tiaozheng.setStack("总量4");
        tiaozheng.setBarWidth("18");
        List<String> tiaozhengData=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
//        for(CollegeInfoEntity c:collageInfo)
//        {
//            collageName.add(c.getCollegeName());
//        }
        for(CollegeInfoEntity c:collageInfo) {
        for(TeacherAttendancePie t:teacherAttendancePie)
        {
                if(c.getCollegeName().equals(t.getName())) {
                    zhengchangData.add(t.getZhengchang().toString());
                    chidaoData.add(t.getChidao().toString());
                    zaotuiData.add(t.getZaotui().toString());
                    tiaozhengData.add(t.getTiaozheng().toString());
                    collageName.add(t.getName());
                }
            }
        }
        chidao.setData(chidaoData);
        zhengchang.setData(zhengchangData);
        zaotui.setData(zaotuiData);
        tiaozheng.setData(tiaozhengData);
        echartsDates.add(zhengchang);
        echartsDates.add(chidao);
        echartsDates.add(zaotui);
        echartsDates.add(tiaozheng);

        map.put("collageName",collageName);
        map.put("data",echartsDates);
        //    map.put("collageRate",collageRate);
        return map;
    }

    @Override
    public Map<String, Object> getTecaherAttendanceCollegeHorizontalComparison(Map<String, Object> params) {
        String valid=ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        List<TeacherAttendancePie>   teacherAttendancePie=teacherPortraitDao.getTecaherAttendanceCollegeHorizontalComparison(params);
        changePercentage(teacherAttendancePie);
        List xAxis=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        List<EchartsDate> echartsDates=new ArrayList<>();
        EchartsDate zhengchang=new EchartsDate();
        zhengchang.setName("正常");
        zhengchang.setStack("总量1");
        zhengchang.setType("line");
        List<String> zhengchangData=new ArrayList<>();
        EchartsDate chidao=new EchartsDate();
        chidao.setName("迟到");
        chidao.setStack("总量2");
        chidao.setType("line");
        List<String> chidaoData=new ArrayList<>();
        EchartsDate zaotui=new EchartsDate();
        zaotui.setName("早退");
        zaotui.setStack("总量3");
        zaotui.setType("line");
        List<String> zaotuiData=new ArrayList<>();
        EchartsDate tiaozheng=new EchartsDate();
        tiaozheng.setName("调整");
        tiaozheng.setType("line");
        tiaozheng.setStack("总量4");
        List<String> tiaozhengData=new ArrayList<>();
        for(TeacherAttendancePie t:teacherAttendancePie)
        {
            xAxis.add(t.getName());
            zhengchangData.add(t.getZhengchang().toString());
            chidaoData.add(t.getChidao().toString());
            zaotuiData.add(t.getZaotui().toString());
            tiaozhengData.add(t.getTiaozheng().toString());

        }
        chidao.setData(chidaoData);
        zhengchang.setData(zhengchangData);
        zaotui.setData(zaotuiData);
        tiaozheng.setData(tiaozhengData);
        echartsDates.add(zhengchang);
        echartsDates.add(chidao);
        echartsDates.add(zaotui);
        echartsDates.add(tiaozheng);

        map.put("xAxis", xAxis);
        map.put("data",echartsDates);
        return map;
    }

    @Override
    public Map<String, List> getCollegeAverageTeacherTime(Map<String, Object> params) {
        String valid=ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        List<TeachLengthTimeEntity> classDiaryList=teacherPortraitDao.getCollegeAverageTeacherTime(params);
        Map<String, List> map=new HashMap<>();
        List<String> xData=new ArrayList<>();
        List<String> seriesData=new ArrayList<>();
        for(int i=0;i<classDiaryList.size();i++)
        {
            xData.add(classDiaryList.get(i).getDate());
            seriesData.add(classDiaryList.get(i).getTeachLength());
        }
        map.put("xData",xData);
        map.put("seriesData",seriesData);
        return map;
    }


    private void changePercentage(List<TeacherAttendancePie>   teacherAttendancePie)
    {
        for(TeacherAttendancePie t:teacherAttendancePie)
        {
            try {
                Double total = t.getChidao() + t.getTiaozheng() + t.getZaotui() + t.getZhengchang();
                BigDecimal chidao = new BigDecimal(t.getChidao() / total * 100);
                BigDecimal zhengchang = new BigDecimal(t.getZhengchang() / total * 100);
                BigDecimal tiaozheng = new BigDecimal(t.getTiaozheng() / total * 100);
                BigDecimal zaotui = new BigDecimal(t.getZaotui() / total * 100);
                t.setChidao(chidao.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                t.setZhengchang(zhengchang.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                t.setTiaozheng(tiaozheng.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                t.setZaotui(zaotui.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            }catch (Exception e) {
                e.printStackTrace();
            }
            }
        }
    }

